﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Data;

public class GameManager : MonoBehaviour
{

    private void Awake()
    {
        AccountHandler.OtherLoginHandler += OtherLogin;
    }
    private void OnDestroy()
    {
        AccountHandler.OtherLoginHandler -= OtherLogin;
    }

    void Start()
    {

        //初始化自己的坦克
        var local = CreateTank(ActorManager.Instance.localUser);
        local.gameObject.AddComponent<LocalPlayer>();
        //初始化相机跟随
        TargetFollow.Instance.Init(local.transform);
        ActorManager.Instance.localTank = local;

        //初始化别的的坦克
        if (ActorManager.Instance.otherUsers != null)
        {
            foreach (var item in ActorManager.Instance.otherUsers)
            {
                CreateTank(item);
            }
        }
    }

    //创建tank工厂
    public Tank CreateTank(Account acc)
    {
        GameObject prefab = Resources.Load<GameObject>("Tank");
        var go = GameObject.Instantiate(prefab);
        Tank tank = go.GetComponent<Tank>();
        tank.Init(acc);

        ActorManager.Instance.AddTank(acc.username, tank);
        return tank;
    }

    //创建新登录的tank
    void OtherLogin(Account user)
    {
        CreateTank(user);
    }
}